package com.example.cpms.controller;

import com.example.cpms.entity.Device;
import com.example.cpms.service.DeviceService;
import com.example.cpms.utils.PageBean;
import com.example.cpms.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Slf4j
@RestController
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/device")  //改用PageHelper插件
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,//前端传入要查询的页数以及每页显示多少条数据
                       String name){ //模糊查询额外传入的值，可选
        log.info("设备分页查询：{}，{},{}",page,pageSize,name); //输出日志信息
        PageBean pageBean = deviceService.page(page,pageSize,name); //把前端传入的信息传到service层
        return Result.success(pageBean);//返回数据
    }


    /**
     *根据id删除设备
     * @return
     */
    @DeleteMapping("/device/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除设备：{}",id);
        deviceService.delete(id);
        return Result.success();
    }

    /**
     * 新增设备
     * @param device
     * @return
     */
    @PostMapping("/device")
    public Result insert(@RequestBody Device device){
        log.info("新增设备：{}",device);//打印日志信息
        deviceService.add(device);
        return Result.success();
    }

    /**
     * 编辑设备信息
     * @param device
     * @return
     */
    @PutMapping("/device")
    public Result updata(@RequestBody Device device){
        log.info("编辑设备：{}",device);
        deviceService.update(device);
        return Result.success();
    }

}
